
#include "gmatlab.h"

// mxArray * g_object_matlab_get_property(GObject *obj, gchar *name);
void mexFunction(int nlhs, mxArray *lhs[], int nrhs, mxArray *rhs[])
{
  GObject *obj;
  gchar *name;

  // Check and convert arguments
  if (nrhs != 2) g_matlab_error("gmatlab:numArg", "g_object_matlab_get_property() takes 2 arguments");
  if (nlhs != 1) g_matlab_error("gmatlab:numRet", "g_object_matlab_get_property() has 1 return value");
  if (mxGetNumberOfElements(rhs[0]) != 1) g_matlab_error("gmatlab:badArray", "g_object_matlab_get_property() Arg 1 must have one element");
  if (mxGetClassID(rhs[0]) != MX_GPOINTER)
    g_matlab_error("gmatlab:badArray", "g_object_matlab_get_property() Arg 1 must be int, not %s", mxGetClassName(rhs[0]));
  obj = ((gpointer*)mxGetData(rhs[0]))[0];
  g_object_matlab_ensure(obj, rhs[0]);
  g_matlab_cleanup_add(g_object_unref, obj);
  if (mxGetClassID(rhs[1]) != mxCHAR_CLASS) g_matlab_error("gmatlab:badType", "g_object_matlab_get_property() Arg 2 must be char, not %s", mxGetClassName(rhs[1]));
  name = g_matlab_mxArray_get_utf8(rhs[1]);
  g_matlab_cleanup_add(g_free, name);
  
  // Call function
  g_matlab_escape_init();
  lhs[0] = g_object_matlab_get_property(obj, name);
  
  g_matlab_escape_clear();
  g_matlab_cleanup();
}
